﻿#region Copyright ©2008-2010, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-05-16 14:58:56 +0200 (Sat, 16 May 2009) $
// Last Changed Revision $Rev: 144 $
//
// ====================================================================================================
#endregion

using System.Xml;
using Tum.CollabXT.TFS.Resources;

namespace Tum.CollabXT.TFS
{
    public class ProcessTemplate : IProcessor
    {
        public void Process(IProcessProvider processProvider, TFSToolProvider toolProvider)
        {
            string processTemplateFileName = toolProvider.OutputPath + "ProcessTemplate.xml";

            #region Load process template document
            toolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_Loading);
            XmlDocument processTemplateDoc = new XmlDocument();
            try
            {
                processTemplateDoc.Load(processTemplateFileName);
            }
            catch
            {
                toolProvider.ProcessLog.AddEntryAndThrowException(string.Format(Language.Error_LoadingFailed, processTemplateFileName));
            }
            #endregion

            toolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_SelectingMetadataNode);
            XmlNode templateMetadataNode = processTemplateDoc.SelectSingleNode("/ProcessTemplate/metadata");
            if (templateMetadataNode == null)
                toolProvider.ProcessLog.AddEntryAndThrowException(Language.Error_NotFound_InvalidTemplate);

            //Get project title and save to template
            toolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_WritingTitle);
            string projectTitle = processProvider.Name;
            if (projectTitle == null)
                toolProvider.ProcessLog.AddEntryAndThrowException(Language.Error_NotFound_InvalidInput);
            toolProvider.ProcessLog.NullCheck<XmlNode>(templateMetadataNode.SelectSingleNode("name"), Language.Error_NotFound_InvalidTemplate).InnerText
                = Helper.PreapareStringForTFS(projectTitle, Helper.TFS_MaxWorkItemTextLen);

            //Get description
            toolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_WritingDescription);
            string projectDescription = processProvider.Description;
            if (projectDescription == null)
            {
                projectDescription = string.Empty;
            }
            toolProvider.ProcessLog.NullCheck<XmlNode>(templateMetadataNode.SelectSingleNode("description"), Language.Error_NotFound_InvalidTemplate).InnerText
                = Helper.PreapareStringForTFS(projectDescription, int.MaxValue);

            #region Save process template
            toolProvider.ProcessLog.AddEntry(Language.ProcessTemplate_Saving);
            try
            {
                processTemplateDoc.Save(processTemplateFileName);
            }
            catch
            {
                toolProvider.ProcessLog.AddEntryAndThrowException(string.Format(Language.Error_SavingFailed, processTemplateFileName));
            }
            #endregion
        }
    }
}
